package com.xsq.wifi.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xsq.wifi.R;
import com.xsq.wifi.activity.UploadVideoActivity;
import com.xsq.wifi.model.VideoItem;
import com.xsq.wifi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 编写人：李振德
 * 创建时间：2025/6/8
 */
public class VideoGridAdapter extends RecyclerView.Adapter<VideoGridAdapter.VideoViewHolder> {
    private List<VideoItem> videoList;
    private Context context;
    private List<VideoItem> selectedVideos = new ArrayList<>(); // 管理选中状态的列表

    public VideoGridAdapter(List<VideoItem> videoList, Context context) {
        this.videoList = videoList;
        this.context = context;
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_video_grid, parent, false);
        return new VideoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        VideoItem video = videoList.get(position);

        // 加载视频缩略图
        Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(
                video.getPath(),
                MediaStore.Images.Thumbnails.MINI_KIND
        );

        if (thumbnail != null) {
            holder.thumbnail.setImageBitmap(thumbnail);
        } else {
            holder.thumbnail.setImageResource(R.mipmap.ic_launcher);
        }

        // 设置选中状态
        holder.checkBox.setChecked(video.isSelected());

        holder.itemView.setOnClickListener(v -> {
            // 切换选中状态
            boolean newSelectedState = !video.isSelected();

            if (newSelectedState) {
                // 尝试选中
                if (selectedVideos.size() < 1) {
                    video.setSelected(true);
                    selectedVideos.add(video);
                } else {
                    ToastUtil.show(context,"最多选择1个视频");
                    return;
                }
            } else {
                // 取消选中
                video.setSelected(false);
                selectedVideos.remove(video);
            }

            // 更新UI
            notifyItemChanged(position);
        });
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }

    public List<VideoItem> getSelectedVideos() {
        return new ArrayList<>(selectedVideos);
    }

    public static class VideoViewHolder extends RecyclerView.ViewHolder {
        ImageView thumbnail;
        CheckBox checkBox;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            thumbnail = itemView.findViewById(R.id.thumbnail);
            checkBox = itemView.findViewById(R.id.checkbox);
        }
    }
}